package tutorial;  
  
import java.awt.BorderLayout;  
import java.awt.Component;  
import java.awt.Container;  
import java.awt.Dimension;  
import java.awt.Image;  
import java.awt.event.ComponentEvent;  
import java.awt.event.ComponentListener;  
import java.awt.event.MouseAdapter;  
import java.awt.event.MouseEvent;  
import java.io.File;  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.List;  
  
import javax.imageio.ImageIO;  
import javax.swing.BoxLayout;  
import javax.swing.ImageIcon;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JPanel;  
/**
 * 本来以为写一个这样的面板会很费劲，
 * 一开始准备改写一个Swing中的布局管理起来完成这样的面板，
 * 但经过仔细考虑还有很多不适合的地方。
 * 于是还是使用ComponentListener监听面板大小变化写了一个比较通用的翻页面板先来看看样子。
 * 似乎不太好看但已经能够完成翻页的任务了，还需要进一步的改进来看看代码是怎样的吧！
 * @author k 2013年8月5日 11:47:02
 *
 */
public class SplitPagePanel extends JPanel implements ComponentListener {  
    private JPanel bodyPanel=new JPanel();  
    private JPanel leftPanel=new JPanel();  
    private JPanel rightPanel=new JPanel();  
    private JPanel topPanel=new JPanel();  
    private JPanel downPanel=new JPanel();  
      
    //分页类  
    private PageResultSet pageResultSet;  
    private List list=new ArrayList();//Button集合    
    private int pageSize=1; //  页大小   
      
    //方向  
    public int axis;  
      
    JLabel fangxiangButton;  
    JLabel fangxiangButton2;  
    JLabel fangxiangButton3;  
    JLabel fangxiangButton4;  
      
    Image fangxiang;  
    Image fangxiang2;  
    Image fangxiang3;  
    Image fangxiang4;  
      
    public SplitPagePanel(int axis){  
        fangxiang =new ImageIcon(getClass().getResource("/images/fangxiang.gif" )).getImage();
		fangxiang2=new ImageIcon(getClass().getResource("/images/fangxiang2.gif")).getImage();
		fangxiang3=new ImageIcon(getClass().getResource("/images/fangxiang4.gif")).getImage();
		fangxiang4=new ImageIcon(getClass().getResource("/images/fangxiang3.gif")).getImage();
		
        //设置面板布局  
        setLayout(new BorderLayout());  
        setOpaque(false);//设置背景透明
        this.axis=axis;  
        //设置工具栏布局  
        bodyPanel.setLayout(new BoxLayout(bodyPanel, axis));  
        leftPanel.setLayout(new BoxLayout(leftPanel, axis));  
        rightPanel.setLayout(new BoxLayout(rightPanel, axis));  
        topPanel.setLayout(new BoxLayout(topPanel, axis));  
        downPanel.setLayout(new BoxLayout(downPanel, axis));  
          
        //分别将面板放置到合适的位置  
        this.add(topPanel,BorderLayout.NORTH);  
        this.add(downPanel,BorderLayout.SOUTH);  
        this.add(leftPanel,BorderLayout.WEST);  
        this.add(rightPanel,BorderLayout.EAST);  
        this.add(bodyPanel,BorderLayout.CENTER);  
        //加入对面板改变监听  
        this.addComponentListener(this);  
    }  
      
    /** 
     * 添加按钮 
     * @param container 
     */  
    public void addButton(Container container){  
        list.add(container);  
    }  
      
    /** 
     * 删除一个按钮 
     * @param container 
     */  
    public void removeButton(Container container){  
        list.remove(container);  
    }  
      
    /** 
     * 删除按钮 
     * @param index 
     */  
    public void removeButton(int index){  
        list.remove(index);  
    }  
      
    /** 
     * 清空所有按钮 
     */  
    public void clreanButton(){  
        list.clear();  
    }  
      
    /** 
     * 获取按钮列表 
     * @return 
     */  
    public List getButtons(){  
        return list;  
    }  
      
    /** 
     * 设置分页 
     */  
    public void setPageResultSet(List lists){  
        pageResultSet=new PageResultSet(lists,1,pageSize);  
    }  
          
    @Override  
    public void componentHidden(ComponentEvent e) {  
        // TODO Auto-generated method stub  
          
    }  
  
    @Override  
    public void componentMoved(ComponentEvent e) {  
        // TODO Auto-generated method stub  
          
    }  
  
    @Override  
    public void componentResized(ComponentEvent e) {  
        if(BoxLayout.X_AXIS==this.axis){  
            int width=bodyPanel.getSize().width;//获取面板宽度  
            int targtWidth=0;  
            int pageNum=list.size();  
            for(int i=0;i<list.size();i++){  
                Container container=(Container)list.get(i);  
                targtWidth+=container.getSize().width;  
                if(targtWidth>=width-5){  
                    pageNum=i;  
                    if (!leftPanel.isAncestorOf(fangxiangButton)) {  
                        fangxiangButton = new JLabel(new ImageIcon(fangxiang));  
                        fangxiangButton.addMouseListener(new MouseAdapter() {  
                              
                            public void mouseReleased(MouseEvent e) {  
                                if (pageResultSet != null) {  
                                    pageResultSet.setCurPage(pageResultSet.previous());  
                                    showButton();  
                                }  
                            }  
  
                        });  
                        leftPanel.add(fangxiangButton);  
                        leftPanel.doLayout();  
                    }  
                      
                    if (!rightPanel.isAncestorOf(fangxiangButton2)) {  
                        fangxiangButton2 = new JLabel(new ImageIcon(fangxiang2));  
                        fangxiangButton2.addMouseListener(new MouseAdapter() {  
                              
                            public void mouseReleased(MouseEvent e) {  
                                if (pageResultSet != null) {  
                                    pageResultSet.setCurPage(pageResultSet.next());  
                                    showButton();  
                                }  
                            }  
                        });  
  
                        rightPanel.add(fangxiangButton2);  
                        rightPanel.doLayout();  
                    }  
                    break;  
                }  
            }  
            if (pageResultSet != null) {  
                pageResultSet.setPageSize(pageNum);  
                pageResultSet.setCurPage(pageResultSet.first());  
                System.out.println(pageResultSet.getPageCount());  
                showButton();  
            }  
        }else if(BoxLayout.Y_AXIS==this.axis){  
            int height=bodyPanel.getSize().height;//获取面板高度  
            int targtHeight=0;  
            int pageNum=list.size();//初始化页面内容数量  
            for(int i=0;i<list.size();i++){  
                Container container=(Container)list.get(i);  
                targtHeight+=container.getSize().height;  
                if(targtHeight>=height-5){//如果累加的高度大于面板的高度  
                    pageNum=i;//则页面只能显示i个内容  
                    if (!topPanel.isAncestorOf(fangxiangButton3)) {  
                        fangxiangButton3 = new JLabel(new ImageIcon(fangxiang3));  
                        fangxiangButton3.addMouseListener(new MouseAdapter() {  
                              
                            public void mouseReleased(MouseEvent e) {  
                                if (pageResultSet != null) {  
                                    pageResultSet.setCurPage(pageResultSet.previous());  
                                    showButton();  
                                }  
                            }  
                        });  
                        topPanel.add(fangxiangButton3);  
                        topPanel.doLayout();  
                    }  
                      
                    if (!downPanel.isAncestorOf(fangxiangButton4)) {  
  
                        fangxiangButton4 = new JLabel(new ImageIcon(fangxiang4));  
                        fangxiangButton4.addMouseListener(new MouseAdapter() {  
                              
                            public void mouseReleased(MouseEvent e) {  
                                if (pageResultSet != null) {  
                                    pageResultSet.setCurPage(pageResultSet.next());  
                                    showButton();  
                                }  
                            }  
                        });  
  
                        downPanel.add(fangxiangButton4);  
                        downPanel.doLayout();  
                      
                    }  
                    break;  
                }  
            }  
            if (pageResultSet != null) {  
                pageResultSet.setPageSize(pageNum);  
                pageResultSet.setCurPage(pageResultSet.getCurPage());  
                showButton();  
            }  
        }  
    }  
      
    /** 
     * 显示按钮 
     */  
    public void showButton(){  
        bodyPanel.removeAll();//先清空bodyPanel面板所有按钮  
          
        //判断当前页是否在第一页  
        if(pageResultSet.getCurPage()==pageResultSet.first()){  
            //如果是则清除左边面板内容和上方面板内容  
            leftPanel.removeAll();  
            leftPanel.doLayout();  
            topPanel.removeAll();  
            topPanel.doLayout();  
        }else{  
            //判断页面内容布局方向  
            if (BoxLayout.X_AXIS == this.axis) {//如果是X轴方向  
                //清除下方面板内容和上方面板内容  
                downPanel.removeAll();  
                downPanel.doLayout();  
                topPanel.removeAll();  
                topPanel.doLayout();  
                //判断左方面板是否有内容  
                if (!leftPanel.isAncestorOf(fangxiangButton)) {  
                    //如果没有则添加左边按钮  
                    fangxiangButton = new JLabel(new ImageIcon(fangxiang));  
                    fangxiangButton.addMouseListener(new MouseAdapter() {  
  
                        public void mouseReleased(MouseEvent e) {  
                            if (pageResultSet != null) {  
                                pageResultSet.setCurPage(pageResultSet  
                                        .previous());  
                                showButton();  
                            }  
                        }  
                    });  
                    leftPanel.add(fangxiangButton);  
                    leftPanel.doLayout();  
                }  
            }  
            //判断页面内容布局方向  
            if(BoxLayout.Y_AXIS==this.axis){//如果是Y轴方向  
                //清除左方面板内容和右方面板内容  
                leftPanel.removeAll();  
                leftPanel.doLayout();  
                rightPanel.removeAll();  
                rightPanel.doLayout();  
                //判断上方面板是否有内容  
                if (!topPanel.isAncestorOf(fangxiangButton3)) {  
                    //如果没有则添加上边按钮  
                    fangxiangButton3 = new JLabel(new ImageIcon(fangxiang3));  
                    fangxiangButton3.addMouseListener(new MouseAdapter() {  
                          
                        public void mouseReleased(MouseEvent e) {  
                            if (pageResultSet != null) {  
                                pageResultSet.setCurPage(pageResultSet.previous());  
                                showButton();  
                            }  
                        }  
  
                    });  
                    topPanel.add(fangxiangButton3);  
                    topPanel.doLayout();  
                }  
            }  
        }  
          
        //判断当前页是否在最后一页  
        if(pageResultSet.getCurPage()==pageResultSet.last()){  
            //如果是则清除右边面板内容和下方面板内容  
            rightPanel.removeAll();  
            rightPanel.doLayout();  
            downPanel.removeAll();  
            downPanel.doLayout();  
        }else{  
            //判断页面内容布局方向  
            if (BoxLayout.X_AXIS == this.axis) {//如果是X轴方向  
                //清除下方面板内容和上方面板内容  
                downPanel.removeAll();  
                downPanel.doLayout();  
                topPanel.removeAll();  
                topPanel.doLayout();  
                //判断右方面板是否有内容  
                if (!rightPanel.isAncestorOf(fangxiangButton2)) {  
                    fangxiangButton2 = new JLabel(new ImageIcon(fangxiang2));  
                    fangxiangButton2.addMouseListener(new MouseAdapter() {  
  
                        public void mouseReleased(MouseEvent e) {  
                            if (pageResultSet != null) {  
                                pageResultSet.setCurPage(pageResultSet.next());  
                                showButton();  
                            }  
                        }  
  
                    });  
  
                    rightPanel.add(fangxiangButton2);  
                    rightPanel.doLayout();  
                }  
            }  
            //判断页面内容布局方向  
            if(BoxLayout.Y_AXIS==this.axis){//如果是Y轴方向  
                //清除左方面板内容和右方面板内容  
                leftPanel.removeAll();  
                leftPanel.doLayout();  
                rightPanel.removeAll();  
                rightPanel.doLayout();  
                //判断右方面板是否有内容  
                if (!downPanel.isAncestorOf(fangxiangButton4)) {  
  
                    fangxiangButton4 = new JLabel(new ImageIcon(fangxiang4));  
                    fangxiangButton4.addMouseListener(new MouseAdapter() {  
                          
                        public void mouseReleased(MouseEvent e) {  
                            if (pageResultSet != null) {  
                                pageResultSet.setCurPage(pageResultSet.next());  
                                showButton();  
                            }  
                        }  
                    });  
  
                    downPanel.add(fangxiangButton4);  
                    downPanel.doLayout();  
                }  
            }  
        }  
          
        List button=(List) pageResultSet.getData();  
        for(int i=0;i<button.size();i++){  
            bodyPanel.add((Component) button.get(i));  
        }  
        bodyPanel.repaint();  
        bodyPanel.doLayout();  
        this.doLayout();  
    }  
  
    @Override  
    public void componentShown(ComponentEvent e) {}  
      
    public static void main(String[] args) {  
  
        JFrame frame=new JFrame();  
        frame.setSize(100,200);  
        //设置好面板内容的方向  
        SplitPagePanel panel=new SplitPagePanel(BoxLayout.X_AXIS);  
        Image ttim = null;  
        Image musicim = null;  
        Image chongwuim = null;  
        Image conrolim = null;  
        Image youxiim = null;  
        Image phoneim = null;  
        try {  
            ttim=ImageIO.read(new File("images/TT.gif"));  
            musicim=ImageIO.read(new File("images/music.gif"));  
            chongwuim=ImageIO.read(new File("images/chongwu.gif"));  
            conrolim=ImageIO.read(new File("images/conrol.gif"));  
            youxiim=ImageIO.read(new File("images/youxi.gif"));  
            phoneim=ImageIO.read(new File("images/phone.gif"));  
        } catch (IOException e1) {  
            // TODO Auto-generated catch block  
            e1.printStackTrace();  
        }  
          
        final JLabel tt=new JLabel(new ImageIcon(ttim));  
        tt.setSize(20,20);  
        tt.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(tt);  
          
        final JLabel music=new JLabel(new ImageIcon(musicim));  
        music.setSize(20,20);  
        music.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(music);  
          
        final JLabel chongwu=new JLabel(new ImageIcon(chongwuim));  
        chongwu.setSize(20,20);  
        chongwu.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(chongwu);  
          
        final JLabel conrol=new JLabel(new ImageIcon(conrolim));  
        conrol.setSize(20,20);  
        conrol.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(conrol);  
          
        final JLabel youxi=new JLabel(new ImageIcon(youxiim));  
        youxi.setSize(20,20);  
        youxi.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(youxi);  
          
        final JLabel phone=new JLabel(new ImageIcon(phoneim));  
        phone.setSize(20,20);  
        phone.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(phone);  
          
        Image tishiim = null;  
        Image souim = null;  
        Image moenyim = null;  
        Image gouwuim = null;  
        Image mailim = null;  
        Image qzronim = null;  
          
        try {  
            tishiim=ImageIO.read(new File("images/tishi.gif"));  
            souim=ImageIO.read(new File("images/sou.gif"));  
            moenyim=ImageIO.read(new File("images/money.gif"));  
            gouwuim=ImageIO.read(new File("images/gouwu.gif"));  
            mailim=ImageIO.read(new File("images/mail.gif"));  
            qzronim=ImageIO.read(new File("images/Qzron.gif"));  
        } catch (IOException e1) {  
            // TODO Auto-generated catch block  
            e1.printStackTrace();  
        }  
          
        JLabel tishi=new JLabel(new ImageIcon(tishiim));  
        tishi.setSize(20,20);  
        tishi.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(tishi);  
          
        final JLabel sou=new JLabel(new ImageIcon(souim));  
        sou.setSize(20,20);  
        sou.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(sou);  
          
        final JLabel moeny=new JLabel(new ImageIcon(moenyim));  
        moeny.setSize(20,20);  
        moeny.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(moeny);  
          
        final JLabel gouwu=new JLabel(new ImageIcon(gouwuim));  
        gouwu.setSize(20,20);  
        gouwu.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(gouwu);  
          
        final JLabel mail=new JLabel(new ImageIcon(mailim));  
        mail.setSize(20,20);  
        mail.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(mail);  
          
        final JLabel qzron=new JLabel(new ImageIcon(qzronim));  
        qzron.setSize(20,20);  
        qzron.setPreferredSize(new Dimension(20, 20));  
        panel.addButton(qzron);  
          
        panel.setPageResultSet(panel.getButtons());  
        panel.doLayout();  
        frame.getContentPane().setLayout(new BorderLayout());  
        frame.getContentPane().add(panel,BorderLayout.CENTER);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setVisible(true);  
      
    }  
}  